//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:2020/9/17
// @Time:5:32 下午
//
//


#include <iostream>
#include <AppManager.h>
#include <csignal>
#include "BaseMod.h"
#include "your_protocol.h"

class EchoMod : public GeeJoan::BaseMod
{
public:
    void init() override
    {
        std::cout << " init echo " << std::endl;
    }

    GeeJoan::ModTypeEnum mod_type() override
    {
        // 如果用自己的协议 服务端与客户端的枚举值要一一对应， 否则解析就错误了
        return static_cast<GeeJoan::ModTypeEnum>(YourModTypeEnum::ModType_EchoMod);
    }

    void procMessage(const GeeJoan::HanderDataType *hander, GeeJoan::StatusCodeTypeEnum &code, std::string &resposeBody) override
    {
        code = GeeJoan::Status_SUCCESS;
        // 回显
        resposeBody = hander->message;
    }
};

using namespace GeeJoan;
AppManager *app;

int main()
{
    try
    {
        AppManager appManager;

        // 注册 方法1
        appManager.registerModFunc(new EchoMod);

        // 方法2
//        appManager.init_before_callback = [](AppManager *app){
//            // 初始化之前注册
//            app->registerModFunc(new EchoMod);
//        };
        //设置端口
        appManager.setPort(10000);
        appManager.setLogPath(".");
        appManager.init_local();

        app = &appManager;

        signal(SIGINT, [](int s)
        {
            std::cout << " exit " << std::endl;
            app->stop();
        });

        signal(SIGTERM, [](int s)
        {
            std::cout << " exit " << std::endl;
            app->stop();
        });
        app->init_network();
        app->run();
    }
    catch (std::exception &e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }
    return 0;
}